﻿
            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Text;
            using System.Threading.Tasks;

namespace ханой
    {
        class Program
        {
            static void Main(string[] args)
            {
        Start:
            try
            {
                int[] tower1 = { 8, 7, 6, 5, 4, 3, 2, 1 };
                int[] tower2 = { 0, 0, 0, 0, 0, 0, 0, 0 };
                int[] tower3 = { 0, 0, 0, 0, 0, 0, 0, 0 };
                int moveCounter = 0;
                do
                {
                    moveCounter++;
                    Console.Clear();
                    Console.WriteLine("Ханойские башни");
                    Console.WriteLine("___________________________________");
                    Console.WriteLine("");
                    Console.WriteLine(" " + tower1[7] + "   " + tower2[7] + "   " + tower3[7]);
                    Console.WriteLine(" " + tower1[6] + "   " + tower2[6] + "   " + tower3[6]);
                    Console.WriteLine(" " + tower1[5] + "   " + tower2[5] + "   " + tower3[5]);
                    Console.WriteLine(" " + tower1[4] + "   " + tower2[4] + "   " + tower3[4]);
                    Console.WriteLine(" " + tower1[3] + "   " + tower2[3] + "   " + tower3[3]);
                    Console.WriteLine(" " + tower1[2] + "   " + tower2[2] + "   " + tower3[2]);
                    Console.WriteLine(" " + tower1[1] + "   " + tower2[1] + "   " + tower3[1]);
                    Console.WriteLine(" " + tower1[0] + "   " + tower2[0] + "   " + tower3[0]);
                    Console.WriteLine("--- --- ---");
                    Console.WriteLine("Номер башни, с которой надо снять кольцо");
                    int startTower = Convert.ToInt32(Console.ReadLine());
                    if (startTower != 1 && startTower != 2 && startTower != 3)
                    {
                        Console.WriteLine("Шулер!");
                        Console.ReadLine();
                        goto Start;
                    }
                    Console.WriteLine("Номер башни, на которую нужно поместить кольцо");
                    int finishTower = Convert.ToInt32(Console.ReadLine());
                    if (finishTower != 1 && finishTower != 2 && finishTower != 3)
                    {
                        Console.WriteLine("Шулер!");
                        Console.ReadLine();
                        goto Start;
                    }

                    int temp = 0;
                    if (startTower == 1)
                    {
                        for (int i = 7; temp == 0; i--)
                        {
                            if (tower1[i] != 0)
                            {
                                temp = tower1[i];
                                tower1[i] = 0;
                            }
                        }
                    }
                    if (startTower == 2)
                    {
                        for (int i = 7; temp == 0; i--)
                        {
                            if (tower2[i] != 0)
                            {
                                temp = tower2[i];
                                tower2[i] = 0;
                            }
                        }
                    }
                    if (startTower == 3)
                    {
                        for (int i = 7; temp == 0; i--)
                        {
                            if (tower3[i] != 0)
                            {
                                temp = tower3[i];
                                tower3[i] = 0;
                            }
                        }
                    }

                    if (finishTower == 1)
                    {
                        for (int i = 0; i < 8; i++)
                        {
                            if (tower1[i] == 0)
                            {
                                tower1[i] = temp;
                                temp = 0;
                                if (tower1[2] > tower1[1] || tower1[1] > tower1[0])
                                {
                                    Console.WriteLine("Перечитай правила!");
                                    Console.ReadLine();
                                    goto Start;
                                }
                            }
                        }
                    }
                    if (finishTower == 2)
                    {
                        for (int i = 0; i < 8; i++)
                        {
                            if (tower2[i] == 0)
                            {
                                tower2[i] = temp;
                                temp = 0;
                                if (tower2[2] > tower2[1] || tower2[1] > tower2[0])
                                {
                                    Console.WriteLine("Перечитай правила!");
                                    Console.ReadLine();
                                    goto Start;
                                }
                            }
                        }
                    }
                    if (finishTower == 3)
                    {
                        for (int i = 0; i < 8; i++)
                        {
                            if (tower3[i] == 0)
                            {
                                tower3[i] = temp;
                                temp = 0;
                                if (tower3[2] > tower3[1] || tower3[1] > tower3[0])
                                {
                                    Console.WriteLine("Перечитай правила!");
                                    Console.ReadLine();
                                    goto Start;
                                }
                            }
                        }
                    }
                }
                while (tower3[0] != 8 || tower3[0] != 7 || tower3[0] != 6 || tower3[0] != 5 || tower3[0] != 4 || tower3[0] != 3 || tower3[1] != 2 || tower3[2] != 1);
                Console.Clear();
                Console.WriteLine("Ханойские башни");
                Console.WriteLine("___________________________________");
                Console.WriteLine("");
                Console.WriteLine(" " + tower1[7] + "   " + tower2[7] + "   " + tower3[7]);
                Console.WriteLine(" " + tower1[6] + "   " + tower2[6] + "   " + tower3[6]);
                Console.WriteLine(" " + tower1[5] + "   " + tower2[5] + "   " + tower3[5]);
                Console.WriteLine(" " + tower1[4] + "   " + tower2[4] + "   " + tower3[4]);
                Console.WriteLine(" " + tower1[3] + "   " + tower2[3] + "   " + tower3[3]);
                Console.WriteLine(" " + tower1[2] + "   " + tower2[2] + "   " + tower3[2]);
                Console.WriteLine(" " + tower1[1] + "   " + tower2[1] + "   " + tower3[1]);
                Console.WriteLine(" " + tower1[0] + "   " + tower2[0] + "   " + tower3[0]);
                Console.WriteLine("--- --- ---");
                Console.WriteLine("  Победа!!!");
                Console.WriteLine("c " + moveCounter + " попыток");
                Console.ReadLine();
            }
            catch (FormatException)
            {
                Console.WriteLine("Аккуратнее следующий раз!");
                Console.ReadLine();
                goto Start;
            }
            }
        }
    }
